//
//  STSpaceViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/9.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "STSpaceViewModel.h"

@interface STSpaceViewModel ()

@property (nonatomic, assign) NSInteger type;

@property (nonatomic, strong) NSArray* tabInfos;

@property (nonatomic, strong) UserDetailData* userDetailData;
@property (nonatomic, strong) ExpertDetailInfoData* expertData;
@property (nonatomic, strong) EnterpriseDetailInfoData* enterpriseData;
@property (nonatomic, strong) InstitutionDetailInfoData* institutionData;

@property (nonatomic, strong) RACCommand* reqDataCmd;

@end

@implementation STSpaceViewModel

#pragma mark - Ovrride
- (void)initialize
{
    [super initialize];
    
    @weakify(self)
    
    _type = -1;
    
    _userID = @"";
    _userType = @"";
    
    _tabInfos = @[];
    
    _reqDataCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
    {
        @strongify(self);
        
        @weakify(self);
        return [[self userDetailSignal:self.userID] then:^RACSignal *{
            
            @strongify(self);
            
            return [self requestSpInfoSignal];
        }];
    }];
}

- (NSArray*)tabInfos
{
    NSArray* result = @[];
    
    if (_type == 0)
    {
        // 个人企业
        result = @[@{@"TabName":@"生态文献"}, @{@"TabName":@"生态行动"}, @{@"TabName":@"生态树"}];
    }
    else if (_type == 1)
    {
        // 专家
        result = @[@{@"TabName":@"简介"}, @{@"TabName":@"生态文献"}, @{@"TabName":@"生态行动"}, @{@"TabName":@"生态树"}];
    }
    else if (_type == 2)
    {
        // 机构
        result = @[@{@"TabName":@"详情"}, @{@"TabName":@"生态文献"}, @{@"TabName":@"生态行动"}, @{@"TabName":@"生态树"}];
    }
    
    return result;
}

- (RACSignal*)userDetailSignal:(NSString*)userID
{
    @weakify(self);
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
    {
        @strongify(self);
        
        @weakify(self);
        [[AFNetWorkUtils racPOSTWthURL:url_interface_userdetail params:interface_param_userdetail(userID)] subscribeNext:^(id x)
        {
            @strongify(self);
            
            UserDetailData* userDetailData = [UserDetailData modelWithJSON:x];
            
            NSInteger identify = [userDetailData.user.identify integerValue];
            NSInteger status = 0;
            NSInteger finalIdentify = 1;
            if (identify > 1)
            {
                status = [userDetailData.content.status integerValue];
                
                if (2 == status)
                {
                    finalIdentify = identify;
                }
            }
            else
            {
                status = [userDetailData.user.status integerValue];
            }
            
            if (finalIdentify == 1)
            {
                self.type = 0;
                self.userType = @"个人用户";
            }
            else if (finalIdentify == 2)
            {
                self.type = 1;
                self.userType = @"专家用户";
            }
            else if (finalIdentify == 3)
            {
                self.type = 0;
                self.userType = @"企业用户";
            }
            else if (finalIdentify == 4)
            {
                self.type = 2;
                NSInteger category = [userDetailData.content.category integerValue];
                NSArray* categorys = @[@"监测单位", @"污染治理单位", @"危废处理单位", @"环保服务单位"];
                self.userType = categorys[category - 1];
            }
            
            self.userDetailData = userDetailData;
            
            [subscriber sendNext:@(finalIdentify == 1)];
            [subscriber sendCompleted];
            
        } error:^(NSError *error)
        {
            [subscriber sendNext:@NO];
            [subscriber sendCompleted];
        }];
        
        return nil;
    }];
}

- (RACSignal*)requestSpInfoSignal
{
    @weakify(self)
    
    NSInteger identify = [self.userDetailData.user.identify integerValue];
    NSInteger status = 0;
    NSInteger finalIdentify = 1;
    if (identify > 1)
    {
        status = [self.userDetailData.content.status integerValue];
        
        if (2 == status)
        {
            finalIdentify = identify;
        }
    }
    
    //个人
    if (finalIdentify == 1)
    {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
        {
            [subscriber sendNext:@YES];
            [subscriber sendCompleted];
            
            return nil;
        }];
    }
    
    //专家
    if (finalIdentify == 2)
    {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                {
                    @strongify(self);
                    
                    NSString* strUserID = self.userID;
                    
                    @weakify(self);
                    [[AFNetWorkUtils racPOSTWthURL:url_interface_expertInfo params:interface_param_expertInfo(strUserID)] subscribeNext:^(id x)
                     {
                         @strongify(self)
                         
                         ExpertDetailInfoData* expertDetailInfo = [ExpertDetailInfoData modelWithJSON:x];
                         
                         self.expertData = expertDetailInfo;
                         
                         NSLog(@"ExpertInfoSignal - Finish");
                         
                         [subscriber sendNext:@YES];
                         [subscriber sendCompleted];
                     }  error:^(NSError *error) {
                         //错误处理
                         
                         [subscriber sendNext:@NO];
                         [subscriber sendCompleted];
                     }];
                    
                    return nil;
                }];
    }
    // 企业
    else if (finalIdentify == 3)
    {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                {
                    @strongify(self);
                    
                    NSString* strUserID = self.userID;
                    
                    @weakify(self);
                    [[AFNetWorkUtils racPOSTWthURL:url_interface_enterpriseInfo params:interface_param_enterpriseInfo(strUserID)] subscribeNext:^(id x)
                     {
                         @strongify(self)
                         
                         EnterpriseDetailInfoData* detailInfo = [EnterpriseDetailInfoData modelWithJSON:x];
                         
                         self.enterpriseData = detailInfo;
                         
                         NSLog(@"UserInfoSignal - Finish");
                         
                         [subscriber sendNext:@YES];
                         [subscriber sendCompleted];
                     }  error:^(NSError *error) {
                         //错误处理
                         
                         [subscriber sendNext:@NO];
                         [subscriber sendCompleted];
                     }];
                    
                    return nil;
                }];
    }
    else if (finalIdentify == 4)
    {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                {
                    @strongify(self);
                    
                    NSString* strUserID = self.userID;
                    
                    @weakify(self);
                    [[AFNetWorkUtils racPOSTWthURL:url_interface_institutionnfo params:interface_param_institutionnfo(strUserID)] subscribeNext:^(id x)
                     {
                         @strongify(self)
                         
                         InstitutionDetailInfoData* institutionDetailInfo = [InstitutionDetailInfoData modelWithJSON:x];
                         
                         self.institutionData = institutionDetailInfo;
                         
                         NSLog(@"UserInfoSignal - Finish");
                         
                         [subscriber sendNext:@YES];
                         [subscriber sendCompleted];
                     }  error:^(NSError *error) {
                         //错误处理
                         
                         [subscriber sendNext:@NO];
                         [subscriber sendCompleted];
                     }];
                    
                    return nil;
                }];
    }
    else
    {
        return [RACSignal empty];
    }
}

@end
